<template>
  <div class="demand-details">
    <div class="title">需求详情</div>
    <div class="form-wrap">
      <div class="inquiry-date f14">
        <div class="date" v-if="info.demand">{{ info.demand.create_time }}</div>
        发起询盘
      </div>
      <m-form :model="formData" v-if="info.demand" label-width="105px" label-position="left">
        <m-form-item label="产品名称:" style="margin-bottom: 0">
          <p class="name f16">{{ info.demand && info.demand.goods_info }}</p>
        </m-form-item>
        <m-form-item label="询价标题:" style="margin-bottom: 0">
          <p class="type f16"> {{info.demand.remarks}} </p>
        </m-form-item>
        <m-form-item label="主要内容:" style="margin-bottom: 0">
          <p class="classifi f16"> {{ info.demand.goods_condition }} </p>
        </m-form-item>
        <m-form-item label="联系人:" style="margin-bottom: 0">
          <p class="quantity f16">{{ info.demand.real_name }}</p>
        </m-form-item>
        <m-form-item label="联系电话:" style="margin-bottom: 0">
          <p class="quantity f16">{{ info.demand.phone }}</p>
        </m-form-item>
        <m-form-item label="产品数量:" style="margin-bottom: 0">
          <p class="quantity f16">{{ info.demand.goods_num }}</p>
        </m-form-item>
        <m-form-item label="产品单位:" style="margin-bottom: 0">
          <p class="quantity f16">{{info.demand.goods_unit == 0 ? "个" : info.demand.goods_unit == 1 ? "件" : "pcs"}}</p>
        </m-form-item>
      </m-form>
      <div class="btns">
        <m-button width="123" type="grey" @click="showDialog = true">取消</m-button>
      </div>
    </div>

    <!-- 商家信息 -->
    <div class="title" v-if="info.demand">商家消息 {{ info.demand.create_time }}</div>
    <!-- 商家回复详情 -->
    <div class="chat-wrap">
      <div class="company-title cursor" @click="toCompanyHomePage" v-if="info.merchant">
        <img :src="info.merchant.mer_avatar" alt="" class="pic" />
        {{ info.merchant.mer_name }}
      </div>
      <div class="recommend-title" v-if="recommendInfo.length">商家推荐商品</div>
      <div class="list">
        <div class="item" v-for="(item, index) in recommendInfo" :key="index">
          <img :src="item.image" alt="" class="cart cursor pic" />
          <p class="name">{{ item.store_name }}</p>
          <div class="price-and-collect">
            <p class="price"><span class="keywords">￥{{ item.price || 0 }}/Pc</span>起</p>
            <img
              v-if="item.is_hide_product != 1"
              src="~@/assets/images/collect-grey.png"
              alt=""
              class="collect cursor"
              @click="collect(item.product_id)"
            />
            <img
              v-else
              src="~@/assets/images/collect-iscot.png"
              alt=""
              class="collect cursor"
              @click="collect(item.product_id)"
            />
          </div>
          <div class="storage-and-cart">
            <p class="storage"><span class="keywords">{{ item.moq || 0 }}</span>MOQ</p>
            <img src="~/assets/images/cart-icon.png" class="cart cursor" @click="joinCart(item.product_id)" />
          </div>
        </div>
      </div>

      <div class="chat">
        <!-- 我的回复 -->
        <div
          v-for="(item, index) in info.demand_content"
          :key="index"
          :class="item.role === 1 ? 'chat-user' : 'chat-merchant'"
        >
          <div
            v-if="item.role === 1"
            style="display: flex; justify-content: flex-end"
          >
            <div class="title-user">我的答复 {{ item.create_time }}</div>
          </div>
          <div v-else style="display: flex; justify-content: flex-start">
            <div class="title-merchant">商家消息 {{ item.create_time }}</div>
          </div>
          <div :class="item.role === 1 ? 'textarea-user' : 'textarea-merchant'">
            {{ item.content }}
          </div>
          <!-- <image :src="item.file"></image> -->
        </div>

        <!-- 回复 -->
        <div class="reply">
          <p class="title">用户回复</p>
          <m-form
            ref="replyForm"
            :model="replyFormData"
            :rules="rules"
            label-width="80px"
            label-position="left"
          >
            <m-form-item prop="annex" style="min-height: 170px" label="附件：">
              <m-upload
                @picList="proPicList"
                :limit="5"
                action="#"
                tip="请上传不超过5张、大小在1M以下的图片，限制格式为PNG、JPG"
              >
                <img
                  class="upload-icon"
                  src="~/assets/images/upload.png"
                  alt=""
                />
              </m-upload>
            </m-form-item>

            <m-form-item prop="reply" label="回复：">
              <m-input
                type="textarea"
                v-model="replyFormData.reply"
                :autosize="{ minRows: 6 }"
              />
            </m-form-item>
            <m-form-item style="margin-top: 80px">
              <div style="display: flex; justify-content: center">
                <m-button width="235" @click="submitForm('ruleForm')">立即提交</m-button>
              </div>
            </m-form-item>
          </m-form>
        </div>
      </div>
    </div>

    <!-- 取消询价验证码验证 -->
    <cancel-demand :showDialog.sync='showDialog' :demandId='id' @handleClose='handleClose' />
  </div>
</template>

<script>
import MButton from "@/components/common/m-button/MButton";
import MForm from "@/components/common/m-form/MForm";
import MFormItem from "@/components/common/m-form/MFormItem";
import MInput from "@/components/common/m-input/MInput";
import MUpload from "@/components/common/m-upload/MUpload";
import MDialog from "@/components/common/m-dialog/MDialog";
import MSelect from "@/components/common/m-select/MSelect";
import MRadio from "@/components/common/m-radio/MRadio";
import { RadioGroup } from "element-ui";
import {  demandMerInfo, demandMerList, apiDemandMessage, collectPro, addCart } from "@/request/api";
import CancelDemand from '../components/cancelDemand.vue'

export default {
  name: "DemandMerInfo",
  components: {
    MUpload,
    MInput,
    MFormItem,
    MForm,
    MButton,
    MDialog,
    MSelect,
    MRadio,
    RadioGroup,
    CancelDemand
  },
  data() {
    return {
      id: "",
      info: {
        class1: "",
        class2: "",
        status: 0,
        merchant:[]
      },
      formData: {},
      showRemaining: false,
      replyFormData: {},
      rules: {
        reply: [{ required: true, message: "请输入回复内容", trigger: "blur" }]
      },
      showDialog: false,
      form: {},
      formLabelWidth: "150px",
      typeOptions: [
        { label: "不需要询价", value: 1 },
        { label: "另外选职", value: 0 }
      ],
      recommendInfo: [],
    };
  },
  created() {
    this.id = this.$route.params.id;
    this.uid = this.$route.query.uid;
    this.getInfo();
  },
  methods: {
    // 获取需求详情
    getInfo() {
      demandMerInfo({id: this.id,uid:this.uid}).then(res => {
        if (res.status === 200) {
          this.info = res.data;
          this.getMerInfoList()
        } else {
          this.$toast.show(res.message, 1000, false);
        }
      }).catch(err => {
        this.$message.error(err.message)
      })
    },
    /** 获取商家推荐和回复列表 */
    getMerInfoList(){
      demandMerList({id: this.id,uid:this.uid}).then(res => {
        if(res.status !== 200) return this.$message.error(res.message)
        this.recommendInfo = res.data.demand_product || [];
        this.info.demand_content = res.data.demand_content || [];
      }).catch(err => {
        this.$message.error(err.message)
      })
    },
    /** 取消询价 */
    handleClose() {
      this.showDialog = false;
      this.$router.push('/profile/purchase-management/my-demand');
    },
    /** 上传图片附件 */
    proPicList(list) {
      this.replyFormData.proFile = list;
    },
    /** 提交用户回复 */
    submitForm() {
      const params = {
        file: String(this.replyFormData.proFile),
        content: this.replyFormData.reply,
        mer_id: this.id,
        demand_id: this.uid
      };
      apiDemandMessage(params).then(res => {
        if (res.status !== 200) return this.$message.error(res.message)
        this.$message.success(res.message);
        this.replyFormData = {};
        this.getMerInfoList();
      }).catch(err => {
        this.$message.error(err.message);
      });
    },
    /** 进入企业详情 */
    toCompanyHomePage() {
      let { href } = this.$router.resolve({
        name: "CompanyHomeHome",
        params: {
          id:this.info.merchant.mer_id
        }
      })
      window.open(href, "_blank")
    },
    /** 收藏产品 */
    collect(id) {
      let that = this;
      if (!this.$store.state.access_token) {
        Toast({
          message: '登录后才可使用收藏和购物车功能！',
          duration: 3000,
          forbidClick: true
        });
        that.$router.push({ name: 'Login' });
      } else {
        collectPro({type_id: id,type: 1}).then(res => {
          if (res.status !== 200) return that.$message.error(res.message)
          that.$toastVant.success(res.message);
          this.getMerInfoList()
        }).catch(err => {
          that.$message.error(err.message);
        });
      }
    },
    /** 加入购物车 */
    joinCart(id) {
      if (!this.$store.state.access_token) {
        Toast({
          message: '登录后才可使用收藏和购物车功能！',
          duration: 3000,
          forbidClick: true
        });
        this.$router.push({ name: 'Login' });
      } else {
        let data = {
          cart_num: 1,
          is_new: 0,
          product_id: id,
          product_type: 0
        };
        addCart(data).then(res => {
          if (res.status !== 200) return this.$message.error(res.message)
          this.$toastVant.success('加入成功!');
        }).catch(err => {
          this.$message.error(err.message);
        });
      }
    },
  }
};
</script>

<style lang="scss" scoped>
@import "~@/assets/style/common.scss";

/deep/ .noBorder .el-button {
  border: none !important;
}

.demand-details {
  margin-top: 30px;
  font-size: 16px;
  color: #666;

  .pics-box {
    display: flex;

    .pic-item {
      width: 160px;
      height: 160px;
      border: 1px solid #eee;
      border-radius: 10px;
      margin-right: 15px;
      overflow: hidden;
      margin-bottom: 20px;
    }
  }

  .number {
    margin-bottom: 20px;
  }

  .notpassed {
    border: solid 1px #eaeaea;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 15px 30px;
    position: relative;
    margin-bottom: 50px;

    .title {
      color: $theme-color-1;
      font-size: 20px;
      font-weight: bold;
      display: flex;
      align-items: center;
      margin-bottom: 25px;

      img {
        margin-right: 10px;
      }
    }

    ._content {
      line-height: 22px;
      margin-bottom: 100px;
    }

    .edit {
      position: absolute;
      right: 15px;
      bottom: 15px;
    }
  }

  > .title {
    width: 250px;
    height: 48px;
    line-height: 48px;
    text-align: center;
    color: #fff;
    background-color: $theme-color-1;
  }

  .form-wrap {
    border: solid 1px #eaeaea;
    padding: 15px 20px;
    position: relative;
    margin-bottom: 50px;

    .fold {
      margin-top: 20px;
      color: $theme-color-1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      cursor: pointer;

      img {
        margin-top: 5px;
      }
    }

    .btns {
      display: flex;
      position: absolute;
      right: 0;
      top: -55px;
    }

    .annex-list {
      display: flex;
      align-items: center;

      .annex {
        width: 100px;
        display: flex;
        flex-direction: column;
        align-items: center;

        .pic {
          width: 34px;
          height: 34px;
        }

        .name {
          font-size: 14px;
          @include text-omitted();
        }
      }
    }

    .inquiry-date {
      position: absolute;
      right: 15px;
      top: 15px;
      color: #969696;
      display: flex;
      flex-direction: column;
      align-items: center;
      background-color: rgb(243, 244, 247);
      padding: 15px;
      border: solid 1px #eaeaea;

      .date {
        margin-bottom: 5px;
      }
    }
  }

  .company-list {
    padding: 15px 20px;
    border: solid 1px #eaeaea;

    .item {
      width: calc((100% - 40px) / 2);
      padding: 15px;
      display: inline-flex;
      align-items: center;
      background-color: rgb(243, 244, 247);
      border: solid 1px #eaeaea;
      margin: 10px;
      .item-wrap {
        height: 80px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      .pic {
        width: 130px;
        height: 80px;
        margin-right: 15px;
      }

      .operation {
        display: flex;
        align-items: center;
        margin-top: 20px;
      }
    }
  }

  .chat-wrap {
    padding: 15px 20px;
    border: solid 1px #eaeaea;

    .company-title {
      display: flex;
      align-items: center;
      margin-bottom: 40px;

      .pic {
        width: 148px;
        height: 76px;
        margin-right: 15px;
        object-fit: cover;
        border: 1px solid #e2e2e2;
      }
    }

    .recommend-title {
      padding-left: 20px;
      border-left: solid 2px #666;
      margin-bottom: 15px;
    }

    .list {
      margin: -10px;

      .item {
        padding: 15px;
        border: solid 1px #eaeaea;
        display: inline-flex;
        flex-direction: column;
        align-items: center;
        width: calc((100% - 80px) / 4);
        margin: 10px;

        .pic {
          width: 170px;
          height: 170px;
          object-fit: cover;
          margin-bottom: 15px;
        }

        .name {
          @include text-omitted(1);
          margin-bottom: 15px;
        }

        .price-and-collect {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 14px;
          color: #969696;
          margin-bottom: 15px;

          .keywords {
            color: $theme-color-1;
            font-weight: bold;
            margin-right: 5px;
          }
        }

        .storage-and-cart {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 14px;
          color: #969696;

          .keywords {
            color: $theme-color-1;
            font-weight: bold;
            margin-right: 5px;
          }
        }
      }
    }

    .chat {
      margin-top: 50px;

      .title-merchant {
        min-width: 266px;
        padding: 0 15px;
        display: inline-block;
        height: 48px;
        line-height: 48px;
        text-align: center;
        color: #fff;
        background-color: rgb(0, 133, 232);
      }

      .title-user {
        min-width: 266px;
        padding: 0 15px;
        display: inline-block;
        height: 48px;
        line-height: 48px;
        text-align: center;
        color: #fff;
        background-color: $theme-color-1;
      }

      .chat-merchant,
      .chat-user {
        &:not(:last-of-type) {
          margin-bottom: 30px;
        }
      }

      .textarea-merchant {
        min-height: 140px;
        padding: 15px 10px;
        border: solid 1px #eaeaea;
        color: #969696;
        line-height: 28px;
        background-color: rgb(243, 244, 247);
      }

      .textarea-user {
        min-height: 140px;
        padding: 15px 10px;
        border: solid 1px #eaeaea;
        color: #969696;
        line-height: 28px;
      }

      .reply {
        margin-top: 120px;
        padding-top: 50px;
        border-top: solid 1px #eaeaea;

        .title {
          color: $theme-color-1;
          margin-bottom: 30px;
          font-weight: bold;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }

  .item-center {
    display: flex;
    justify-content: center;
    align-items: center;

    > p {
      margin: 0 10px;
    }

    > p:nth-child(2) {
      text-decoration: underline;
      cursor: pointer;
    }
  }
}
</style>
